package com.example.erhuo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.erhuo.Good;
import com.example.erhuo.R;
import com.example.erhuo.api.NetAPI;

import java.io.IOException;
import java.util.LinkedList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.Response;

public class GoodAdapter extends BaseAdapter {

    private static String TAG = "GoodAdapter";
    private LinkedList<Good> mData;
    private Context mContext;

    public GoodAdapter(LinkedList<Good> mData, Context mContext) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_good, parent, false);
        }
        ImageView img_icon = (ImageView) convertView.findViewById(R.id.good_icon);
        TextView txt_title = (TextView) convertView.findViewById(R.id.good_title);
        TextView txt_price = (TextView) convertView.findViewById(R.id.good_price);
        TextView txt_time = (TextView) convertView.findViewById(R.id.good_time);
        Good good = mData.get(position);
        if (good.getImage() == null) {
            Bitmap bitmap = BitmapFactory.decodeResource(this.mContext.getResources(), R.drawable.dog_empty);
            good.setImage(bitmap);
            img_icon.setImageBitmap(bitmap);
        } else {
            img_icon.setBackgroundColor(Color.parseColor("#00000000"));
            img_icon.setImageBitmap(good.getImage());//todo

        }
        txt_price.setText(good.getPrice());
        txt_title.setText(good.getTitle());
        txt_time.setText(good.getUploadTime());
        return convertView;
    }

    public void addGood(Good good) {
        mData.add(good);
    }

    public void clear() {
        mData.clear();
    }
}
